<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="handleExport">导出</el-button>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button
          type="text"
          icon="el-icon-view"
          size="small"
          plain
          style="border: 0; background-color: transparent !important"
          @click.stop="handleView(row)"
          v-if="permission.allocationManage_view"
          >详情
        </el-button>
      </template>
    </avue-crud>
    <el-dialog append-to-body :close-on-click-modal="false" :title="title" :visible.sync="showDialog" v-if="showDialog" width="60%" top="7vh" @close="reset">
      <div>领料单号：</div>
      <avue-crud
        :option="sheetOption"
        :table-loading="detailLoading"
        :data="sheetData"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="sheetForm"
        ref="addTableCrud"
        :key="timer"
        @selection-change="selectionDetailChange"
      >
      </avue-crud>
    </el-dialog>
    <export-comp ref="exportComp" page-api-url="mes-warehouse/outbound-order/page-query" :option="option" :page="page" :query="exportParams" file-name="待出库单"> </export-comp>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove } from '@/api/warehouse/outbound-order';
import { mapGetters } from 'vuex';
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
import exportComp from '@/components/export-comp';
import { filterEmptyValues } from '@/util/util';
export default {
  components: {
    CommonModalTemplate,
    userTableConfig,
    exportComp
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'OUTBOUND_ORDER',
      form: {},
      sheetForm: {},
      loading: false,
      detailLoading: false,
      title: '',
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      exportParams: {},
      selectionList: [],
      showDialog: false,
      data: [],
      sheetData: [],
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuPosition: 'right',
        searchMenuSpan: 18,
        dialogClickModal: false,
        border: true,
        height: 'calc(100vh - 440px)',
        viewBtn: false,
        addBtn: false,
        selection: true,
        printBtn: false,
        editBtn: false,
        delBtn: false,
        index: true,
        indexLabel: '序号',
        menu: true,
        menuWidth: 100,
        columnBtn: false,
        searchSpan: 5,
        column: [
          {
            label: '单据类型',
            prop: 'sheetType',
            type: 'select',
            search: true,
            searchSpan: 6,
            searchOrder: 1,
            dicUrl: '/api/blade-system/dict/dictionary?code=outbound_order_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '单号',
            prop: 'sheetNo',
            search: true,
            searchSpan: 6
          },
          {
            label: '工单号',
            prop: 'workOrderNo',
            search: true,
            searchSpan: 6
          },
          {
            label: '产品编码',
            prop: 'productPartNo',
            search: true,
            searchSpan: 6
          },
          {
            label: '产品型号',
            prop: 'productModel',
            search: true,
            searchSpan: 6
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            dataType: 'string',
            dicUrl: '/api/blade-system/dict/dictionary?code=outbound_order_status',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          }
        ]
      },
      sheetOption: {
        tip: false,
        searchShow: true,
        searchSpan: 4,
        searchMenuSpan: 6,
        dialogClickModal: false,
        height: '400',
        border: true,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        columnBtn: false,
        cancelBtn: false,
        saveBtn: false,
        selection: false,
        menu: false,
        page: false,
        index: true,
        indexLabel: '序号',
        refreshBtn: false,
        expandBtn: false,
        column: [
          {
            label: '物料编码',
            prop: 'partNo'
          },
          {
            label: '物料名称',
            prop: 'partName'
          },
          {
            label: '物料型号',
            prop: 'partModel'
          },
          {
            label: '工段',
            prop: 'sectionName'
          },
          { label: '内部批次', prop: 'lotNo' },
          {
            label: '需求数量',
            prop: 'requireQuantity'
          },
          {
            label: '实发数量',
            prop: 'actualQuantity'
          },
          {
            label: '发料状态',
            prop: 'status',
            type: 'select',
            dataType: 'string',
            dicUrl: '/api/blade-system/dict/dictionary?code=outbound_order_detail_status',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.allocationManage_view, false)
      };
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.selectionClear();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleView(row) {
      this.title = '详情';
      this.showDialog = true;
      this.sheetData = row.outboundOrderDetailList;
    },
    handleExport() {
      this.exportParams = Object.assign(filterEmptyValues(this.query));
      this.$refs.exportComp.visible = true;
    }
  }
};
</script>

<style lang="scss" scoped></style>
